package cn.hx.mallorder.controller;

import cn.hx.mallorder.dao.OmsOrderDao;
import cn.hx.mallorder.entity.OmsOrder;
import cn.hx.mallorder.service.OmsOrderItemService;
import cn.hx.mallorder.service.OmsOrderService;
import cn.hx.mallorder.util.JwtUtils;
import cn.hx.malluser.vo.UmsMemberVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.jsonwebtoken.Claims;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@Controller
public class PayController {


    private final SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Resource
    private OmsOrderDao omsOrderDao;
    @Resource
    private OmsOrderService omsOrderService;
    @Resource
    private ProductClient productClient;
    @Resource
    private OmsOrderItemService omsOrderItemService;
    @Resource
    RedisTemplate<String,Object> redisTemplate;


    public Long getUserId(HttpServletRequest request){
        Long id=0L;
        Cookie[] cookies = request.getCookies();
        if (cookies!=null&&cookies.length>0){
            for (Cookie cookie : cookies) {
                if ("token".equals(cookie.getName())){
                    String value = cookie.getValue();
                    if (JwtUtils.verifyToken(value)){
                        Claims claims = JwtUtils.getClaims(value);
                        id = Long.valueOf(claims.get("data", Map.class).get("id").toString());
                    }
                }
            }
        }
        return id;
    }

    @GetMapping("obligation")
    public String findAll(Model model, Integer time, HttpSession session, HttpServletRequest request) throws ParseException {
//        List<OmsOrder> pays = omsOrderDao.findByPays();
//        List<OmsOrder> list = new ArrayList<>();
//        Date date = new Date();
//        Calendar instance = Calendar.getInstance();
//        instance.setTime(date);
//        if (time!=null){
//            if (time==1){
//                instance.set(Calendar.YEAR,instance.get(Calendar.YEAR)-1);
//                Date date1 = instance.getTime();
//                for (OmsOrder pay : pays) {
//                    if (pay.getCreateTime().after(date1)) {
//                        list.add(pay);
//                    }
//                }
//            }
//            if (time==2){
//                instance.set(Calendar.MONTH,instance.get(Calendar.MONTH)-3);
//                Date date1 = instance.getTime();
//                for (OmsOrder pay : pays) {
//                    if (pay.getCreateTime().after(date1)) {
//                        list.add(pay);
//                    }
//                }
//            }
//            if (time==0){
//                model.addAttribute("pays",pays);
//                return "obligation";
//            }
//        } else {
//            System.out.println("pays:"+pays);
//            model.addAttribute("pays",pays);
//            return "obligation";
//        }
//        omsOrderService.put();

        Long userId = getUserId(request);
        List<OmsOrder> pays = omsOrderService.get(""+userId);
        model.addAttribute("pays",pays);
        return "obligation";
    }

    @GetMapping("cancelOrder")
    @ResponseBody
    public Integer cancel(Long id,HttpServletRequest request){
        Long userId = getUserId(request);
        Integer integer = omsOrderService.deleteOrder(userId, id);
        if (integer==1){
            return 1;
        }
        return 0;
    }
    @GetMapping("findBill")
    @ResponseBody
    public OmsOrder bill(Long id){
        System.out.println(id);

        return omsOrderDao.findAllById(id);
    }

    @GetMapping("count")
    @ResponseBody
    public Integer getCount(HttpServletRequest request){
        Long userId = getUserId(request);
        List<OmsOrder> omsOrders = omsOrderService.get(""+userId);
        int size = omsOrders.size();
        return size;
    }



}
